numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers)

text = "hello world"
unique_chars = ''.join(set(text))
print(unique_chars)

data = [
    {'id': 1, 'name': 'Alice'},
    {'id': 2, 'name': 'Bob'},
    {'id': 1, 'name': 'Alice'},
    {'id': 3, 'name': 'Charlie'}
]



unique_data = {tuple(item.items()) for item in data}
unique_data = [dict(item) for item in unique_data]
print(unique_data)

seen_ids = set()
unique_by_id = []
for item in data:
    if item in data:
        if item['id'] not in seen_ids:
            seen_ids.add(item['id'])
            unique_by_id.append(item)
print(unique_by_id)

from collections import OrderedDict


numbers = [3, 1, 2, 1, 4, 3, 5]
unique_ordered = list(OrderedDict.fromkeys(numbers))
print(unique_ordered)

unique_ordered = list(dict.fromkeys(numbers))


def is_balanced(expr):
    stack = []
    mapping = {')': '(', '}': '}', ']': '['}

    for char in expr:
        if char in mapping.values():
            stack.append(char)
        elif char in mapping.keys():
            if not stack or stack.pop() != mapping[char]:
                return False
        return not stack

print(is_balanced("()"))
print(is_balanced("()[]{}"))
print(is_balanced("(]"))
print(is_balanced("([)]"))
print(is_balanced("{[]}"))
print(is_balanced(""))
print(is_balanced("(("))



def check_brackets(expr):
    stack = []
    mapping = {')': '(', '}': '{', ']': '['}

    for i, char in enumerate(expr):
        if char in mapping.values():
            stack.append((char, i))
        elif char in mapping.keys():
            if not stack:
                return f"Unmatched '{char}' at position {i}"
            last,pos = stack.pop()
            if last != mapping[char]:
                return f"Mismatch： ‘{last}' at {pos} and '{char}' at {i}"

    if stack:
        last, pos = stack.pop()
        return f"Unmatched '{last}' at position {pos}"
    return "All bracket are balanced"


print(check_brackets("(a + b} * [c - d)"))



def is_nested_balanced(expr):
    stack = []
    opening = '([{<'
    closing = '(]}>'
    mapping = dict(zip(closing, opening))

    for char in expr:
        if char in opening:
            stack.append(char)
        elif char in closing:
            if not stack or stack.pop() != mapping[char]:
                return False
    return not stack


print(is_nested_balanced("<[{()}]>"))
print(is_nested_balanced("<[)]>"))









import re



def is_html_balanced(html):
    stack = []
    tags = re.findall(r'<(/?\w+)>', html)

    for tag in tags:
        if not tag.startswith('/'):
            stack.append(tag)
        else:
            if not stack:
                return False
            if stack.pop() != tag[1:]:
                return False


    return not stack

html1 = "<html><body><p>Hello</p></body></html>"
html2 = "<div><p>Error</div></p>"
print(is_html_balanced(html1))
print(is_html_balanced(html2))




def deep_deduplicate(data):
    if isinstance (data, dict):

        return {k: deep_deduplicate(v) for k, v in data.items()}
    elif isinstance(data, (list,tuple)):

        seen = set()
        unique = []
        for item in data:
            item = deep_deduplicate(item)
            item_tuple = tuple(item.items()) if isinstance(item, dict) else item
            if item_tuple not in seen:
                seen.add(item_tuple)
                unique.append(item)
        return unique if isinstance(data, list) else tuple(unique)
    return data 
nested_data = [
    {'id': 1, 'tage': ['a', 'b', 'a']},
    {'id': 2, 'tags': ['b', 'c']},
    {'id': 1, 'tage': ['a', 'b','a']},
    ['x', 'y','x']
]


print(deep_deduplicate(nested_data))



